/**
 * @project mybatisDemo_zkingbbsxiugai
 * @Title MyInterceptor.java
 * @description 
 * @Package org.qiuqiu.interceptor
 * @author wangsongmao  email:wangsongmao@zkingsoft.com
 * @date  2015年5月16日 下午5:27:44
 * 
 */
package org.qiuqiu.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.qiuqiu.vo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**@project mybatisDemo_zkingbbsxiugai
 * @Title MyInterceptor.java
 * @Package org.qiuqiu.interceptor
 * @author wangsongmao  email:wangsongmao@zkingsoft.com
 * @date  2015年5月16日 下午5:27:44
 */
public class MyInterceptor implements HandlerInterceptor{

	private Logger log=Logger.getLogger(this.getClass());
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		/*  rs.sendRedirect("login");*/
		 System.out.println("拦截的地址===========postHandle()");
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	public boolean preHandle(HttpServletRequest re, HttpServletResponse rs,
			Object o) throws Exception {
		   boolean flag = false;
	        String url = re.getRequestURL().toString();
	        User user=(User) re.getSession().getAttribute("user");	     
	        if (user!=null) {
	        	 log.info("拦截的地址========"+url);
				flag=true;
			}else{   
	        String basePath = re.getScheme() + "://" + re.getServerName() + ":"
					+ re.getServerPort() + re.getContextPath() + "/";
	        log.info("basePath=="+basePath);
	       /* re.getRequestDispatcher(basePath+"login").forward(re, rs);*/
	       	      rs.sendRedirect(basePath+"login");
			}
		return flag;
	}

}
